<template>
  <el-card class="spu">
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showSaveSpu"
    >
      添加Spu
    </el-button>

    <el-table v-loading="loading" :data="spuList" border class="spu-table">
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column prop="spuName" label="Spu名称" />
      <el-table-column prop="description" label="Spu描述" />
      <el-table-column label="操作" width="250">
        <template v-slot="{ row }">
          <el-tooltip content="添加SKU" placement="top" effect="light">
            <el-button
              type="primary"
              :icon="Plus"
              size="small"
              @click="showSaveSku(row)"
            />
          </el-tooltip>
          <el-tooltip content="修改SPU" placement="top" effect="light">
            <el-button
              type="warning"
              :icon="Edit"
              size="small"
              @click="showUpdateSpu(row)"
            />
          </el-tooltip>
          <el-tooltip content="查看SKU列表" placement="top" effect="light">
            <el-button type="info" :icon="InfoFilled" size="small" />
          </el-tooltip>
          <el-tooltip content="删除SPU" placement="top" effect="light">
            <el-button type="danger" :icon="Delete" size="small" />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9]"
      :small="true"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getSpuList"
      @current-change="getSpuList"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, InfoFilled, Delete } from "@element-plus/icons-vue";
import { getSpuListApi } from "@/api/product/spu";
import type { SpuList, SpuFormData } from "@/api/product/model/spuModel";
import { useCategoryStore } from "@/stores/category";
import { ElMessage } from "element-plus";

// 声明接受自定义事件
const emit = defineEmits(["setIsShow", "setSpu"]);

// 显示添加SPU
const showSaveSpu = () => {
  emit("setIsShow", 2);
  emit("setSpu", {
    spuName: "",
    tmId: undefined,
    description: "",
    spuImageList: [],
    spuSaleAttrList: [],
  });
};

// 显示修改SPU
const showUpdateSpu = (row: SpuFormData) => {
  emit("setIsShow", 2);
  emit("setSpu", {
    ...row,
    // 默认row中数据是null，null在更新组件使用会报错
    spuImageList: [],
    spuSaleAttrList: [],
  });
};

// 显示添加SKU
const showSaveSku = (row: SpuFormData) => {
  emit("setIsShow", 3);
  emit("setSpu", row);
};

/******************** SpuList数据展示 **********************/

const loading = ref(false);
const spuList = ref<SpuList>([]);

const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);

const categoryStore = useCategoryStore();

// 获取Spu列表
const getSpuList = async () => {
  loading.value = true;
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
  loading.value = false;
  ElMessage.success("获取SPU列表成功");
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);
</script>

<style scoped>
.spu {
  margin-top: 20px;
}
.spu-table {
  margin: 10px 0;
}
</style>
